const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    totalMoney: 0,
    isEdit: false,
    cartList: [],
    // 选中的checkbox
    selectedArr: [],
    // 选择全部
    allCheck: false
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCartList();
  },

  changeStatus() {
    this.setData({
      isEdit: !this.data.isEdit
    })
  },

  // 更新购买数量
  updateNum(event) {
    let url = '/cart/updateNum';
    let {
      cartid,
      index
    } = event.currentTarget.dataset;
    let buyNum = event.detail;
    let data = {
      cartId: cartid,
      buyNum
    }
    app.post(url, data).then(res => {
      let item = {
        ...this.data.cartList[index],
        buyNum
      }
      let list = this.data.cartList;
      list.splice(index, 1, item);
      this.setData({
        cartList: list
      })
      this.setTotalMoney();
    }).catch(err => {
      console.log(err);
    })
  },



  // 提交
  submit() {
    // 编辑状态,删除购物车
    if (this.data.isEdit) {
      this.delCart();
      return false;
    }

    let list = this.data.cartList;
    let cartId = list.filter(item => this.data.selectedArr.includes(item.cartId)).map(item => item.cartId);
    let totalMoney = this.data.totalMoney;
    let data = {
      cartId,
      totalMoney
    };
    console.log(data);
    let url = "/preOrder/add";
    app
      .post(url, data)
      .then(res => {
        wx.navigateTo({
          url: "/pages/order/orderConfirm/index?preOrderId=" +
            res.result.preOrderId
        });
      })
      .catch(err => {
        console.log(err);
      });
  },

  // 删除购物车
  delCart() {
    let url = '/cart/del';
    let cartId = this.data.selectedArr;

    app.post(url, {
      cartId
    }).then(res => {
      console.log(res);
      // 移除对应的那条数据
      let list = this.data.cartList.filter(item => !cartId.includes(item.cartId));
      this.setData({
        cartList: list
      })
      this.setTotalMoney();
    }).catch(err => {
      console.log(err);
    })
  },

  // 获取购物车列表
  getCartList() {
    let url = "/cart/all";
    app
      .get(url)
      .then(res => {
        this.setData({
          cartList: res.data
        });
        this.setTotalMoney();
      })
      .catch(err => {
        console.log(err);
      });
  },

  // 单选
  groupChange(event) {
    let selectedArr = event.detail;
    let allCheck = selectedArr.length === this.data.cartList.length;
    this.setData({
      selectedArr,
      allCheck
    });
    this.setTotalMoney();
  },

  // 全选
  selectAll(event) {
    let allCheck = event.detail;
    let selectedArr = [];
    if (allCheck) {
      selectedArr = this.data.cartList.map(item => item.cartId);
    }
    this.setData({
      allCheck,
      selectedArr
    });
    this.setTotalMoney();
  },

  // 计算总金额
  setTotalMoney() {
    let totalMoney = this.data.cartList.filter(item => this.data.selectedArr
        .includes(item.cartId))
      .map(item => item.price * item.buyNum).reduce((a, b) => a + b, 0);
    this.setData({
      totalMoney
    })
    return totalMoney;
  }
});